package com.wisdom.admin.controller;

import com.wisdom.admin.common.api.CommonPage;
import com.wisdom.admin.common.api.CommonResult;
import com.wisdom.admin.component.CancelOrderSender;
import com.wisdom.admin.dto.UmsAdminLoginParam;
import com.wisdom.admin.dto.UmsAdminParam;
import com.wisdom.admin.dto.UmsMenuNode;
import com.wisdom.admin.mbg.model.UmsAdmin;
import com.wisdom.admin.mbg.model.UmsAdminMenu;
import com.wisdom.admin.service.UmsAdminMenuService;
import com.wisdom.admin.service.UmsAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.*;


/**
 * @author ming
 * @ClassName UmsAdminController.java
 * @Description 用户
 * @createTime 2021年03月12日 23:38:00
 */
@Api(tags = "UmsAdminController", description = "后台用户")
@Controller
@RequestMapping(value = "/admin")
public class UmsAdminController {
    private static final Logger LOGGER = LoggerFactory.getLogger(UmsAdminController.class);

    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;

    @Autowired
    UmsAdminService umsAdminService;

    @Autowired
    UmsAdminMenuService umsAdminMenuService;

    @Autowired
    private CancelOrderSender cancelOrderSender;

    @ApiOperation("登录")
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public CommonResult login(@Validated @RequestBody UmsAdminLoginParam umsAdminLoginParam, BindingResult result)
    {
        String token = umsAdminService.login(umsAdminLoginParam.getUsername(), umsAdminLoginParam.getPassword());
        if (token == null){
            return CommonResult.validateFailed("用户名或密码错误");
        }
        Map<String,String> tokenMap = new HashMap<>();
        tokenMap.put("token",token);
        tokenMap.put("tokenHead",tokenHead);
        return CommonResult.success(tokenMap);
    }

    @ApiOperation("用户注册")
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    @ResponseBody
    public CommonResult register(@Validated @RequestBody UmsAdminParam umsAdminParam,BindingResult result)
    {
        UmsAdmin admin = umsAdminService.register(umsAdminParam);
        if (admin == null){
            return CommonResult.failed();
        }
        return CommonResult.success(admin);
    }

    @ApiOperation("获取当前登录用户信息")
    @RequestMapping(value = "/info",method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getAdminInfo(Principal principal)
    {
        if (principal == null){
            return CommonResult.unauthorized(null);
        }
        String username = principal.getName();
        UmsAdmin admin = umsAdminService.getAdminByUsername(username);
        if (admin == null){
            return CommonResult.failed();
        }
        Map<String, Object> data = new HashMap<>();
        data.put("username",admin.getUsername());
        data.put("icon",admin.getIcon());
        data.put("email",admin.getEmail());
        data.put("nickName",admin.getNickName());
        return CommonResult.success(data);
    }

    @ApiOperation("订单延时任务demo")
    @RequestMapping(value = "/mq",method = RequestMethod.POST)
    @ResponseBody
    public CommonResult testMq()
    {
        Map<String,Object> order = new HashMap<>();
        order.put("orderId", 155);
        order.put("time",new Date());
        order.put("customerName",new String("小明"));

        //获取订单超时时间，假设为1分钟(1min = 60000ms)
        long delayTimes = 60000;
        cancelOrderSender.sendMessage(order,delayTimes);
        return CommonResult.success(true);
    }

    @ApiOperation("后台菜单栏")
    @RequestMapping(value = "/menu",method = RequestMethod.GET)
    @ResponseBody
    public CommonResult menuLists()
    {
        List<UmsMenuNode> lists = umsAdminMenuService.getLists();
        return CommonResult.success(lists);
    }

    @ApiOperation("后台管理员列表")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public CommonResult list(@RequestParam(value = "keyword", required = false) String keyword,
                             @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                             @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum)
    {
        List<UmsAdmin> list = umsAdminService.getUserLists(keyword,pageSize,pageNum);
        return CommonResult.success(CommonPage.restPage(list));
    }
}
